package com.example.permissionsystem1_1.businessFunction.service.interfaces.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.permissionsystem1_1.businessFunction.dao.entity.po.*;
import com.example.permissionsystem1_1.businessFunction.dao.repository.FunctionInfoDao;
import com.example.permissionsystem1_1.businessFunction.dao.repository.RoleFunctionDao;
import com.example.permissionsystem1_1.businessFunction.dao.repository.UserFunctionRecordDao;
import com.example.permissionsystem1_1.businessFunction.dao.repository.UserInfoDao;
import com.example.permissionsystem1_1.businessFunction.service.converter.FunctionInfoServiceConverter;
import com.example.permissionsystem1_1.businessFunction.service.converter.UserInfoServiceConverter;
import com.example.permissionsystem1_1.businessFunction.service.entity.reqModel.functionInfoReqModel.*;
import com.example.permissionsystem1_1.businessFunction.service.entity.reqModel.userInfoReqModel.*;
import com.example.permissionsystem1_1.businessFunction.service.entity.rspModel.functionInfoRspModel.*;
import com.example.permissionsystem1_1.businessFunction.service.entity.rspModel.userInfoRspModel.*;
import com.example.permissionsystem1_1.businessFunction.service.interfaces.FunctionInfoService;
import com.example.permissionsystem1_1.businessFunction.service.interfaces.UserInfoService;
import com.example.permissionsystem1_1.common.businessFunction.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class FunctionInfoServiceImpl extends BaseServiceImpl implements FunctionInfoService {
    @Autowired
    private FunctionInfoDao functionInfoDao;
    @Autowired
    private RoleFunctionDao roleFunctionDao;
    @Autowired
    private UserFunctionRecordDao userFunctionRecordDao;

    @Override
    public FunctionInfoInsertRspModel insert(FunctionInfoInsertReqModel reqModel) {
        FunctionInfoPo po= FunctionInfoServiceConverter.convertReqModelToPo(reqModel);
        Integer resultInt=-1;
        if(po!=null){
            resultInt=functionInfoDao.insert(po);
        }
        return FunctionInfoServiceConverter.covertToRspModel(resultInt);
    }

//    @Override
//    public TestInsertBatchRspModel insertBatch(TestInsertBatchReqModel reqModel) {
//        List<TestPo> pos=TestServiceConverter.convertReqModelToPo(reqModel);
//        Integer resultSum=-1;
//        if(pos!=null&&pos.size()>0){
//            resultSum=testDao.insertBatch(pos);
//        }
//        return TestServiceConverter.covertToInsertBatchRspModel(resultSum);
//    }

    @Override
    public FunctionInfoDeleteByIdRspModel deleteById(FunctionInfoDeleteByIdReqModel reqModel) {
        FunctionInfoPo po= FunctionInfoServiceConverter.convertReqModelToPo(reqModel);
        return FunctionInfoServiceConverter.covertToDeleteByIdRspModel(functionInfoDao.deleteById(po.getId()));
    }

//    @Override
//    public TestDeletesRspModel deletes(TestDeletesReqModel reqModel) {
//        TestPo po= TestServiceConverter.convertReqModelToPo(reqModel);
//        Integer resultSum=-1;
//        if(po!=null){
//            resultSum=testDao.deletes(po);
//        }
//        return TestServiceConverter.covertToDeletesRspModel(resultSum);
//    }

//    @Override
//    public TestDeleteBatchByIdsRspModel deleteBatchByIds(TestDeleteBatchByIdsReqModel reqModel) {
//        TestPo po=TestServiceConverter.convertReqModelToPo(reqModel);
//        Integer resultSum=-1;
//        if(po!=null&&po.getIds()!=null&&po.getIds().size()>0){
//            resultSum=testDao.deleteBatchByIds(po.getIds());
//        }
//        return TestServiceConverter.covertToDeleteBatchByIdsRspModel(resultSum);
//    }

    @Override
    @Transactional
    public FunctionInfoUpdateByIdRspModel updateById(FunctionInfoUpdateByIdReqModel reqModel) {
        FunctionInfoPo po= FunctionInfoServiceConverter.convertReqModelToPo(reqModel);
        Integer resultInt=-1;
        if(po!=null){
            resultInt=functionInfoDao.updateById(po);
        }
        //关联数据清理:根据功能id删除当前功能的角色功能关联记录(超管角色不会受影响)
        deleteRoleFunctionByFunctionId(po);
        //功能权限的重建:根据功能id删除用户功能的全部功能(admin账号除外)
        reconstructionUserFunctionsByFunctionId(po);
        return FunctionInfoServiceConverter.covertToUpdateByIdRspModel(resultInt);
    }

//    @Override
//    public TestUpdatesRspModel updates(TestUpdatesReqModel reqModel) {
//        TestPo po= TestServiceConverter.convertReqModelToPo(reqModel);
//        Integer resultSum=-1;
//        if(po!=null&&po.getByPo()!=null&&po.getSetPo()!=null){
//            resultSum=testDao.updates(po.getByPo(),po.getSetPo());
//        }
//        return TestServiceConverter.covertToUpdatesRspModel(resultSum);
//    }

//    @Override
//    public TestUpdateBatchByIdsRspModel updateBatchByIds(TestUpdateBatchByIdsReqModel reqModel) {
//        TestPo po= TestServiceConverter.convertReqModelToPo(reqModel);
//        Integer resultSum=-1;
//        if(po!=null&&po.getIds()!=null&&po.getIds().size()>0){
//            resultSum=testDao.updateBatchByIds(po);
//        }
//        return TestServiceConverter.covertToUpdateBatchByIdsRspModel(resultSum);
//    }

    @Override
    public FunctionInfoSelectByIdRspModel selectById(FunctionInfoSelectByIdReqModel reqModel) {
        FunctionInfoPo po= FunctionInfoServiceConverter.convertReqModelToPo(reqModel);
        if(po!=null&&po.getId()!=null){
            po=functionInfoDao.selectById(po.getId());
        }
        return FunctionInfoServiceConverter.covertToSelectByIdRspModel(po);
    }

    @Override
    public FunctionInfoSelectsRspModel selects(FunctionInfoSelectsReqModel reqModel) {
        FunctionInfoPo po= FunctionInfoServiceConverter.convertReqModelToPo(reqModel);
        List<FunctionInfoPo> pos=new ArrayList<>();
        if(po!=null){
            pos=functionInfoDao.selects(po);
        }
        return FunctionInfoServiceConverter.covertToSelectsModel(pos);
    }

//    @Override
//    public TestSelectBatchByIdsRspModel selectBatchByIds(TestSelectBatchByIdsReqModel reqModel) {
//        TestPo po= TestServiceConverter.convertReqModelToPo(reqModel);
//        List<TestPo> pos=new ArrayList<>();
//        if(po!=null){
//            pos=testDao.selectBatchByIds(po.getIds());
//        }
//        return TestServiceConverter.covertToSelectBatchByIdsModel(pos);
//    }

    @Override
    public FunctionInfoSelectPageRspModel selectPage(FunctionInfoSelectPageReqModel reqModel) {
        FunctionInfoPo po=FunctionInfoServiceConverter.convertReqModelToPo(reqModel);
        if(po==null){
            return null;
        }
        Page page=new Page(reqModel.getPageNo(),reqModel.getPageSize());
        //默认按照年龄排序
        page.addOrder(OrderItem.asc("id"));
        IPage<FunctionInfoPo> iPagePo=functionInfoDao.selectPage(page,po);
        return FunctionInfoServiceConverter.covertToSelectPageModel(iPagePo);
    }

    //**********本类的业务处理方法
    /** 关联数据清理:根据功能id删除当前功能的角色功能关联记录(超管角色除外) */
    private void deleteRoleFunctionByFunctionId(FunctionInfoPo po){
        if(!"1".equals(po.getState())){
            return;//对功能权限的启用不用管,因为相当于创建了一个新功能,所以肯定没有角色功能的删除
        }
        RoleFunctionPo roleFunctionPo=new RoleFunctionPo();
        roleFunctionPo.setFunctionId(po.getId());
        //不会删除roleId=1的超管角色的功能权限
        roleFunctionDao.deletesExcept(roleFunctionPo);
    }
    /** 功能权限的重建:根据功能id删除用户功能的全部功能(admin账号除外) */
    private void reconstructionUserFunctionsByFunctionId(FunctionInfoPo po){
        if(!"1".equals(po.getState())){
            return;//对功能权限的启用不用管,因为相当于创建了一个新功能,所以肯定没有用户功能存有新创建的功能
        }
        UserFunctionRecordPo userFunctionRecordPo=new UserFunctionRecordPo();
        userFunctionRecordPo.setFunctionId(po.getId());
        //不会删除userId=1的admin账号的功能权限
        userFunctionRecordDao.deletesExcept(userFunctionRecordPo);
    }


}
